// stores/counter.js
import { defineStore } from 'pinia'

const localDate = localStorage.getItem('v3pz')

export const useCounterStore = defineStore('menu', {
  
  state: () => ( {
    
    isCollapsed: false,
    // selectMenu: ['首页'],
    selectMenu: [],
    // routerList: localDate ? [] : localDate.routerList 
    routerList: localDate ? localDate.routerList : []
  
  }),
  actions: {
    toggleCollapsed() {
        this.isCollapsed = !this.isCollapsed
    },
    // meta: { id: '1', name: '控制台', icon: 'Platform', path: '/dashboard', describe: '用于展示当前系统中的统计数据、统计报表及重要实时数据' }
    // payload: { id: '1', name: '控制台', icon: 'Platform', path: '/dashboard', describe: '用于展示当前系统中的统计数据、统计报表及重要实时数据' }
    addMenu(payload){

        // 去重操作
        if(this.selectMenu.findIndex(item => item.path === payload.path) === -1){
          this.selectMenu.push(payload)
        }
        
        // console.log('！！！！！', this.selectMenu);

    },
    closeMenu(payload){
      const index = this.selectMenu.findIndex(item => item.name === payload.name)
      this.selectMenu.splice(index, 1)
    },
    dynamicMenu( payload) {
      const modules= import.meta.glob('../views/**/**/*.vue')
      // console.log("modules",modules);

      console.log(payload, 'payload');
      
      
      function routerSet(router) {
        router.forEach(route => {
          // 判断没有子菜单，拼接路由数据
          if (!route.children) {
            const url = `../views${route.meta.path}/index.vue`;
            route.component = modules[url]
          } else {
            routerSet(route.children)
          }
        })
      }
      routerSet(payload)
      this.routerList = payload
    }

  },
  getters: {
    // doubleCount: (state) => state.count * 2
  },
  persist: {
      key: 'v3pz',
      storage: localStorage,
      paths: ['routerList']
      // paths: ['menuList', 'activeMenu'] // 只持久化 menuList 和 activeMenu 状态
  }
})